﻿Namespace Prim.Borc

	Public Class Borc

		Public Bordrolar As New List(Of Bordro)
		Public Aylar As New SortedList(Of Date, Ay)
		Public Donemler As New SortedList(Of String, Donem)
		Public OdemeTarihi As Date
		Public Token As Token
		Public ErrorMessage As String

		Class Yatirim

			Public sno As Integer
			Public kimlik As String
			Public adi As String
			Public onadi As String
			Public soyadi As String
			Public pogs As Integer
			Public pek As Decimal
			Public basamak As Integer

		End Class

		Class Bordro

			Public aktif As Boolean = True
			Public virtual As Boolean
			Public tarih As Date
			Public tip As String
			Public isyeri As String
			Public yatirimlar As New List(Of Yatirim)
			Public pek As Decimal
			Public anapara As Decimal
			Public pogs As Integer
			Public ceza As Decimal
			Public cezaoran As Decimal
			Public indceza As Decimal
			Public basamak As Decimal
			Public temp As New SortedList(Of String, Decimal)

			Sub hesapla(TehlikeSinifi As Integer, OdemeTarihi As Date)
				Dim Oran As Decimal = Prim.Oran(tarih, tip, TehlikeSinifi)
				anapara = pek * Oran / 100
				Dim GecikmeZammi As Decimal = Prim.GecikmeZammiHesabi(tip, tarih, OdemeTarihi)
				ceza = anapara * GecikmeZammi / 100
			End Sub

		End Class

		Class Donem

			Private _property As EventArgs
			Public Donem As String
			Public OdemeTarihi As Date
			Public Aylar As New List(Of Ay)
			Public PesinDurumu As String

			ReadOnly Property Sum(Key As String)
				Get
					Dim Toplam As Decimal = 0
					For Each Ay In Aylar
						Toplam += Ay.Sum(Key)
					Next
					Return Toplam
				End Get
			End Property

			ReadOnly Property Anapara As Decimal
				Get
					Dim Temp As Decimal = 0
					For Each Ay In Aylar
						Temp += Ay.Anapara
					Next
					Return Temp
				End Get
			End Property

			ReadOnly Property Ceza As Decimal
				Get
					Dim Temp As Decimal = 0
					For Each Ay In Aylar
						Temp += Ay.Ceza
					Next
					Return Temp
				End Get
			End Property

			ReadOnly Property Aktif As Boolean
				Get
					For Each Ay In Aylar
						If Ay.Aktif Then Return True
					Next
					Return False
				End Get
			End Property

			ReadOnly Property Toplam As Decimal
				Get
					Return Anapara + Ceza
				End Get
			End Property

		End Class

		Class Ay

			Public Tarih As Date
			Public CalisilanGun As Integer
			Public Tehlike As Decimal
			Public Basamak As Integer
			Public Rapor As New SortedList(Of String, Integer)
			Public Prim As New SortedList(Of String, Integer)
			Public Borc As New SortedList(Of String, Integer)
			Public Bordrolar As New List(Of Bordro)

			Sub New()
				For Each Item In "has.isk.ana".Split(".")
					Rapor.Add(Item, 0)
				Next
				For Each Item In "D.D1.D2.H.H1.E.J.L.L1.X.Y".Split(".")
					Prim.Add(Item, 0)
				Next
				For Each Item In "H.H1.J.L.X.Y.T".Split(".")
					Borc.Add(Item, 0)
				Next
			End Sub

			ReadOnly Property Sum(Key As String)
				Get
					Dim Toplam As Decimal = 0
					For Each Bordro In Bordrolar
						If Bordro.aktif Then
							Toplam += Bordro.temp(Key)
						End If
					Next
					Return Toplam
				End Get
			End Property

			Function YatırılmasiGerekenGun() As Integer
				Return CalisilanGun - (Rapor("has") + Rapor("ana") + Rapor("isk"))
			End Function

			Function YeterliGunVar() As Boolean
				If YatırılmasiGerekenGun() <= 0 Then Return True
				If Prim("H") + Prim("H1") + Prim("J") + If(Prim("D") + Prim("D1") = 0, 0, Prim("L") + Prim("L1")) >= YatırılmasiGerekenGun() Then Return True
				Return False
			End Function

			ReadOnly Property Anapara As Decimal
				Get
					Dim Temp As Decimal = 0
					For Each Bordro In Bordrolar
						If Bordro.aktif Then Temp += Bordro.anapara
					Next
					Return Temp
				End Get
			End Property

			ReadOnly Property Ceza As Decimal
				Get
					Dim Temp As Decimal = 0
					For Each Bordro In Bordrolar
						If Bordro.aktif Then Temp += Bordro.ceza
					Next
					Return Temp
				End Get
			End Property

			ReadOnly Property Aktif As Boolean
				Get
					For Each Bordro In Bordrolar
						If Bordro.aktif Then Return True
					Next
					Return False
				End Get
			End Property

			ReadOnly Property Toplam As Decimal
				Get
					Return Anapara + Ceza
				End Get
			End Property

		End Class

	End Class

	Class BorcIsveren : Inherits Borc

		Dim sno As Integer
		Dim sube As Integer

		Sub Hesapla(sno As Integer, sube As Integer, tarih1 As Date, tarih2 As Date, odemetar As Date)
			Me.sno = sno
			Me.sube = sube
			Me.OdemeTarihi = odemetar
			TablolariOku()
			If GirisCikis.Rows.Count = 0 Then
				ErrorMessage = "Sigortalının işveren statüsünde kaydı bulunamadı."
				Return
			End If
			'Tüm aylar için borç hesapla
			Dim tarih As Date = tarih1
			Do Until tarih > tarih2
				BorcHesaplaAyIcin(tarih, odemetar)
				tarih = tarih.AddDays(1 - tarih.Day).AddMonths(1)
			Loop
			'Aylık borçları dönemlere ayır
			For Each Ay In Aylar.Values
				Dim Donem As String = Trim(Ay.Tarih.Year) & "/" & (Ay.Tarih.Month - 1) \ 3 + 1
				If Not Donemler.ContainsKey(Donem) Then Donemler.Add(Donem, New Donem)
				Donemler(Donem).Donem = Donem
				Donemler(Donem).Aylar.Add(Ay)
			Next
		End Sub

		Sub BorcHesaplaAyIcin(ByVal tarih As Date, odemetar As Date)
			Dim Interval As New Interval(tarih, tarih.AddMonths(1).AddDays(-tarih.Day))
			If CalismaDonemleri.Count > 0 Then
				Dim Found As Boolean = False
				For Each Donem In CalismaDonemleri
					If Interval.Tarih1 <= Donem("tarih2") And Interval.Tarih2 >= Donem("tarih1") Then
						Found = True
						If Interval.Tarih1 < Donem("tarih1") Then Interval.Tarih1 = Donem("tarih1")
						If Interval.Tarih2 > Donem("tarih2") Then Interval.Tarih2 = Donem("tarih2")
					End If
				Next
				If Not Found Then Return
			End If
			Dim Isyeri = AyIcinIsyeri(Interval)
			If Isyeri Is Nothing Then Return 'İlgili dönemde işveren kaydı yok
			Dim tip As String = If(Isyeri("statu") Like "01.11.*", "H1", "H")
			Dim Ay As New Ay
			Ay.Tarih = tarih
			Ay.CalisilanGun = Interval.OverlappingDays(New Interval(Isyeri("Tarih1"), Isyeri("Tarih2")))
			Ay.Tehlike = Isyeri("tehlike")
			For Each Row As DataRow In Raporlar.Rows
				Ay.Rapor(Row("tur")) += Interval.OverlappingDays(New Interval(Row("Tarih1"), Row("Tarih2")))
			Next
			For Each Row As DataRow In Yatirimlar.Select("bordrotar = '" & tarih.ToString("yyyy-MM-01") & "'").Rows
				If Ay.Prim.ContainsKey(Row("bordro")) Then Ay.Prim(Row("bordro")) += Row("pogs")
			Next
			Ay.Basamak = BasamakBul(tarih, Len(tip))
			If Ay.YeterliGunVar Then
				'Fark kontrolü
				Dim yatirim = Yatirimlar.Select("bordrotar = '" & YMDS(tarih) & "'")
				If yatirim.EOF Then Return
				If GirisCikis.Select("isyerino='" & yatirim("isyerino") & "'").EOF Then Return
				Dim tehlike = GirisCikis.Select("isyerino='" & yatirim("isyerino") & "'")("tehlike")
				If Ay.Tehlike > tehlike Then
					Dim Bordro As New Bordro
					Dim Oran As Decimal = (Ay.Tehlike - tehlike) / 2
					Dim Anapara As Decimal = Prim.Tutar(Token, tarih, Len(tip), Ay.Basamak, odemetar) * yatirim("pogs") / 30 * Oran / 100
					Dim CezaOran As Decimal = Prim.GecikmeZammiHesabi("H", tarih, odemetar)
					Dim Ceza As Decimal = CezaOran * Anapara / 100
					Bordro.tarih = tarih
					Bordro.tip = "T"
					Bordro.isyeri = yatirim("isyerino")
					Bordro.anapara = Anapara
					Bordro.cezaoran = CezaOran
					Bordro.ceza = Ceza
					Bordro.pogs = yatirim("pogs")
					Ay.Bordrolar.Add(Bordro)
					Ay.Borc("T") = yatirim("pogs")
					'Ay.Tehlike = Ay.Tehlike - tehlike
					Aylar.Add(Ay.Tarih, Ay)
				End If
				Return
			End If
			If tarih < EmeklilikTar And Ay.Prim("L") = 0 And (Ay.Prim("D") + Ay.Prim("D1") + Ay.Prim("D2")) = 0 Then
				Ay.Borc(tip) = Ay.YatırılmasiGerekenGun - Ay.Prim(tip)
			ElseIf tarih >= EmeklilikTar And Ay.Prim("L") = 0 And Ay.Prim("E") = 0 And Ay.Prim("D") = 0 Then
				Ay.Borc("J") = Ay.YatırılmasiGerekenGun - Ay.Prim("J")
			Else
				If tarih < EmeklilikTar Then
					Ay.Borc("L") = Ay.Prim("D") + Ay.Prim("D1") + Ay.Prim("D2") - Ay.Prim("L") - Ay.Prim("L1")
					If Ay.Borc("L") > Ay.YatırılmasiGerekenGun Then Ay.Borc("L") = Ay.YatırılmasiGerekenGun
					Ay.Borc(tip) = Ay.YatırılmasiGerekenGun - Ay.Borc("L") - Ay.Prim("L") - Ay.Prim(tip)
				Else
					Ay.Borc("L") = Ay.Prim("E") + Ay.Prim("D") + Ay.Prim("D1") + Ay.Prim("D2") - Ay.Prim("L") - Ay.Prim("L1")
					Ay.Borc("J") = Ay.YatırılmasiGerekenGun - Ay.Borc("L") - Ay.Prim("L") - Ay.Prim("J") - Ay.Prim(tip)
					Ay.Borc("Y") = Ay.Prim("H")
				End If
			End If
			For Each BordroTipi As KeyValuePair(Of String, Integer) In Ay.Borc
				If BordroTipi.Value > 0 Then
					If Not (sube <> 0 And (BordroTipi.Key = "H" Or BordroTipi.Key = "L" Or BordroTipi.Key = "J") AndAlso Mid(Isyeri("isyerino"), 1, 1) <> sube) Then
						Dim Oran As Decimal = Prim.Oran(tarih, BordroTipi.Key, Ay.Tehlike)
						Dim Pek = Prim.Tutar(Token, tarih, Len(tip), Ay.Basamak, odemetar) * BordroTipi.Value / 30
						Dim Anapara As Decimal = Pek * Oran / 100
						Dim CezaOran As Decimal = Prim.GecikmeZammiHesabi(BordroTipi.Key, tarih, odemetar)
						Dim Ceza As Decimal = CezaOran * Anapara / 100
						Dim Bordro As New Bordro
						Bordro.tarih = tarih
						Bordro.tip = BordroTipi.Key
						Bordro.isyeri = Isyeri("isyerino")
						Bordro.pek = Pek
						Bordro.anapara = Anapara
						Bordro.ceza = Ceza
						Bordro.cezaoran = CezaOran
						Bordro.pogs = BordroTipi.Value
						Bordro.basamak = Ay.Basamak
						Ay.Bordrolar.Add(Bordro)
					End If
				End If
			Next
			If Ay.Bordrolar.Count = 0 Then Return
			Aylar.Add(Ay.Tarih, Ay)
		End Sub

		Dim Yatirimlar As Recordset
		Dim Raporlar As Recordset
		Dim GirisCikis As Recordset
		Dim Basamaklar As Recordset
		Dim Maaslar As Recordset
		Dim CalismaDonemleri As Recordset

		Dim DogumTar, EmeklilikTar As Date
		Dim AraTarih(1) As String

		Sub TablolariOku()
			Dim Rs As Recordset = Sigortali.SnoAra(Token, sno)
			DogumTar = Rs("dtar")
			Q("select isyerino,bordrotar,bordro,pek,pogs from shesk where sno = @sno order by 1")
			Yatirimlar = Token.OpenSQL("*", sno)
			Q("select * from (")
			Q("select 'has' as tur, histbastar as tarih1, histsontar as tarih2 from shasrapor where sno = @sno union all")
			Q("select 'isk' as tur, histbastar as tarih1, histsontar as tarih2 from siskrapor where sno = @sno union all")
			Q("select 'ana' as tur, odebastar as tarih1, odesontar as tarih2 from sagecode where sno = @sno")
			Q(") t where tarih1 is not null and tarih2 is not null")
			Raporlar = Token.OpenSQL("*", sno)
			Q("select a.repsube,a.isyerino,girciktar as tarih1,c.tehlike,a.statu,")
			Q("isnull((select min(girciktar) from sigc b where a.sno = b.sno and a.isyerino = b.isyerino")
			Q("and a.girciktar < b.girciktar and b.gircik = 'C' and a.statu = b.statu),'2099/01/01') as tarih2")
			Q("from sigc a inner join isyerigenel c on a.isyerino = c.isyerino")
			Q("where sno = @sno and statu in ('01.01.003','01.01.004','01.11.003','01.11.004') and gircik = 'G'")
			GirisCikis = Token.OpenSQL("*", sno)
			Q("select convert(datetime,'19760101') as tarih, 1 as basamak from sgenel where sno = @sno union all")
			Q("select bastar, basamak from sgelirbas where sno = @sno order by tarih")
			Basamaklar = Token.OpenSQL("*", sno)
			Q("select * from yaslılık..yasmaasbil a inner join yashesaptur b on a.hturu = b.hturu")
			Q("where a.kimlik = @kimlik and b.myo = 'Y' and (a.odeyerkod < 900 or a.odeyerkod = 999)")
			CalismaDonemleri = Hukuk.Af.CalismaDonemleriOku(Token, sno)
			Maaslar = Token.OpenSQL("*", Rs("kimlik"))
			EmeklilikTar = If(Maaslar.EOF, "2099/01/01", IsNull(Maaslar("ymbastar"), Token.OpenSQL("select min(convert(datetime,ltrim(yil*10000+ay*100+1))) from yaslılık..yasmaas where ay <> 13 and kimlik = @kimlik", Rs("kimlik"))(0)))
		End Sub

		Private Function AyIcinIsyeri(Interval As Interval) As DataRow
			Dim Isyeri As DataRow = Nothing
			For Each Row As DataRow In GirisCikis.Rows
				Dim IsInterval As New Interval(Row("Tarih1"), Row("Tarih2"))
				If Interval.OverlappingDays(IsInterval) > 0 Then
					Dim Pick As Boolean = False
					If Isyeri Is Nothing Then Pick = True
					If Not Pick AndAlso Row("tehlike") > Isyeri("tehlike") Then Pick = True
					If Not Pick AndAlso Row("tehlike") = Isyeri("tehlike") AndAlso Row("repsube") = sube Then Pick = True
					If Pick Then Isyeri = Row
				End If
			Next
			Return Isyeri
		End Function

		Private Function BasamakBul(tarih As Date, yasa As Integer) As Integer
			Dim Basamak As Integer
			For Each Row As DataRow In Basamaklar.Rows
				If Row("tarih") > tarih.AddMonths(1).AddDays(-1) Then Exit For
				Dim YeniBasamak = CInt(IsNull(Row("basamak"), 1))
				If Not (Basamak >= 1 And YeniBasamak < 1) Then Basamak = YeniBasamak
			Next
			If yasa = 1 AndAlso Basamak < 6 And DogumTar.AddYears(31) <= tarih Then Basamak = 6
			If Basamak = 0 Then Basamak = 1
			Return Basamak
		End Function

	End Class

	Class BorcIsyeri : Inherits Borc

		Dim isyerino As String

		Dim GirisCikis As Recordset
		Dim DigerGirisCikis As Recordset
		Dim Raporlar As Recordset
		Dim Yatirimlar As Recordset
		Dim DigerYatirimlar As Recordset
		Dim ProblemliYatirimlar As Recordset
		Dim Basamaklar As Recordset
		Dim Kisiler As Recordset
		Dim Isyeri As DataRow
		Dim MaxBordroTarihIsci As String
		Dim MaxBordroTarihIsveren As String
		Dim BordroTarihi As Date
		Dim Statu As String
		Dim Kisi As DataRow
		Dim DateStr As String
		Dim Tarih1, Tarih2 As Date

		Sub Start(isyerino As String, odemetar As Date)
			Me.isyerino = isyerino
			Me.OdemeTarihi = odemetar
			Isyeri = Token.OpenSQL("select * from isyerigenel where isyerino = @isyerino", isyerino).Rows(0)
		End Sub

		Sub Hesapla(isyerino As String, tarih1 As Date, tarih2 As Date, odemetar As Date)
			Start(isyerino, odemetar)
			TablolariOku()
			Dim T As Date = Today.AddDays(-20).AddMonths(-1)
			T = T.AddDays(1 - T.Day)
			MaxBordroTarihIsci = T.ToString("yyyyMM")
			T = T.AddMonths(-2)
			MaxBordroTarihIsveren = Trim(T.Year) & Format(((T.Month - 1) \ 3) * 3 + 3, "00")
			For Each Row As DataRow In GirisCikis.Rows
				Dim Donem1 As String = CDate(Row("tarih1")).ToString("yyyyMM")
				Dim Donem2 As String = CDate(Row("tarih2")).ToString("yyyyMM")
				If Donem1 <= tarih1.ToString("yyyyMM") Then Donem1 = tarih1.ToString("yyyyMM")
				If Donem2 >= tarih2.ToString("yyyyMM") Then Donem2 = tarih2.ToString("yyyyMM")
				BordroTarihi = ToDate(Donem1)
				Dim Isveren = Row("statu") = "01.01.003" Or Row("statu") = "01.01.004" Or Row("statu") = "01.11.003" Or Row("statu") = "01.11.004"
				If Not Isveren Then
					If Isveren And Donem2 > MaxBordroTarihIsveren Then Donem2 = MaxBordroTarihIsveren
					If Not Isveren And Donem2 > MaxBordroTarihIsci Then Donem2 = MaxBordroTarihIsci
					Statu = Row("statu")
					Do Until BordroTarihi.ToString("yyyyMM") > Donem2
						Me.Tarih1 = BordroTarihi
						Me.Tarih2 = Me.Tarih1.AddMonths(1).AddDays(-1)
						If Me.Tarih1 < Row("tarih1") Then Me.Tarih1 = Row("tarih1")
						If Me.Tarih2 > Row("tarih2") Then Me.Tarih2 = Row("tarih2")
						DigerGirisCikis.CreateSubset("sno=" & Row("sno"))
						Raporlar.CreateSubset("sno=" & Row("sno"))
						Yatirimlar.CreateSubset("sno=" & Row("sno"))
						DigerYatirimlar.CreateSubset("sno=" & Row("sno"))
						ProblemliYatirimlar.CreateSubset("sno=" & Row("sno"))
						Basamaklar.CreateSubset("sno=" & Row("sno"))
						If Not Kisiler.Select("sno=" & Row("sno")).EOF Then
							Kisi = Kisiler.Select("sno=" & Row("sno")).Rows(0)
							BorcHesaplaKisiDonem()
						End If
						BordroTarihi = BordroTarihi.AddMonths(1)
					Loop
				End If
			Next
			TutarlariHesapla()
			'Aylık borçları dönemlere ayır
			For Each Ay In Aylar.Values
				Dim Donem As String = Trim(Ay.Tarih.Year) & "/" & Format(Ay.Tarih.Month, "00")
				If Not Donemler.ContainsKey(Donem) Then Donemler.Add(Donem, New Donem)
				Donemler(Donem).Donem = Donem
				Donemler(Donem).Aylar.Add(Ay)
			Next
		End Sub

		Private Sub BorcHesaplaKisiDonem()
			Dim SonGun As Date = BordroTarihi.AddMonths(1).AddDays(-1)
			Dim AyGunSayisi As Integer = SonGun.Day
			Dim Gunler As New Collection
			Dim Emekli As Boolean = Kisi("maasbasdrm") = "E" AndAlso Kisi("maasbtar") <= BordroTarihi
			'Yatırılması gereken bordro tipini bul
			Dim Bordro As String = ""
			Select Case Statu
				Case "01.01.001" : Bordro = "A"
				Case "01.01.002" : Bordro = "D"
				Case "01.01.003", "01.01.004" : Bordro = "H"
				Case "01.01.008" : Bordro = "C"
				Case "01.11.001", "01.11.002", "01.11.012", "01.11.014", "01.11.015", "01.11.022" : Bordro = "D1"
				Case "01.11.009", "01.11.018", "01.11.016" : Bordro = "D2"
				Case "01.11.003", "01.11.004" : Bordro = "H1"
				Case "01.11.005" : Bordro = "K1"
				Case "01.11.008" : Bordro = "C1"
				Case Else : MsgBox("Beklenmeyen bir statü koduna rastlandı: " & Statu) : Exit Sub
			End Select
			If Emekli Then
				Select Case Bordro
					Case "D" : Bordro = "E"
					Case "D1", "D2" : Bordro = "E1"
					Case "H" : Bordro = "J"
					Case "H1" : Bordro = "J1"
				End Select
			End If
			'Yatırım gereken gün sayısını bul
			Dim GunSayisi As Integer = If(Tarih2 = SonGun, 30 - Tarih1.Day + 1, Tarih2.Day)
			'Raporlu gün sayısını bul
			Dim Raporlu As Integer = 0
			Dim Tarih As Date = Tarih1
			Do Until Tarih > Tarih2
				DateStr = "'" & Tarih.ToString("yyyy/MM/dd") & "'"
				If Not RaporuYoksa() Then Raporlu += 1
				Tarih = Tarih.AddDays(1)
			Loop
			GunSayisi -= Raporlu
			'Yatırım yapılması gerekmiyorsa çık
			If GunSayisi <= 0 Then Exit Sub
			If Raporlu = AyGunSayisi Then Exit Sub 'Şubat için
			'Yeterli normal yatırımı varsa çık
			DateStr = "'" & BordroTarihi.ToString("yyyy/MM/dd") & "'"
			Dim Yatirilan As Integer = YatirilanGunSayisi(Bordro)
			If Bordro = "D" Then Yatirilan += YatirilanGunSayisi("E") + YatirilanGunSayisi("C")
			If Bordro = "E" Then Yatirilan += YatirilanGunSayisi("D") + YatirilanGunSayisi("C")
			If Yatirilan > 0 Then Return
			'Yeterli problemli yatırımı varsa uyar
			Dim Problemli As Integer = ProblemliGunSayisi(Bordro)
			If Problemli > 0 Then Return
			If GunSayisi = 1 Then Return
			BorcEkle(BordroTarihi, Kisi("kimlik"), Kisi("sno"), Kisi("adi"), Kisi("onadi"), Kisi("soyadi"), GunSayisi - Yatirilan, Bordro)
		End Sub

		Sub BorcEkle(tarih As Date, kimlik As String, sno As Integer, adi As String, onadi As String, soyadi As String, pogs As Integer, tip As String)
			If Not Aylar.ContainsKey(tarih) Then Aylar.Add(tarih, New Ay)
			Dim Ay = Aylar(tarih)
			Ay.Tarih = tarih
			Dim Bordro As Bordro = Nothing
			For Each b In Ay.Bordrolar
				If b.tip = tip Then Bordro = b : Exit For
			Next
			If Bordro Is Nothing Then
				Bordro = New Bordro
				Ay.Bordrolar.Add(Bordro)
				Bordro.tip = tip
			End If
			Bordro.tarih = tarih
			Dim Yatirim As New Yatirim
			Yatirim.kimlik = kimlik
			Yatirim.sno = sno
			Yatirim.adi = adi
			Yatirim.onadi = onadi
			Yatirim.soyadi = soyadi
			Yatirim.pogs = pogs
			Bordro.yatirimlar.Add(Yatirim)
		End Sub

		Private Function BasamakBul() As Integer
			Dim Basamak As Integer = 0
			Do Until Basamaklar.Subset.EOF
				If Basamaklar.Subset("tarih") > BordroTarihi.AddMonths(1).AddDays(-1) Then Exit Do
				Basamak = Basamaklar.Subset("basamak")
				Basamaklar.Subset.MoveNext()
			Loop
			If Basamak < 6 AndAlso Kisi("dtar").AddYears(31) <= BordroTarihi Then Basamak = 6
			If Basamak = 0 Then Basamak = 1
			Return Basamak
		End Function

		Private Function DahaTehlikeliIsteCalisiyorsa() As Boolean
			Return DigerGirisCikis.Subset.Exists(DateStr & " >= tarih1 and " & DateStr & " <= tarih2 and statu in ('01.01.003','01.01.004') and tehlike > " & Isyeri("tehlike"))
		End Function

		Private Function RaporuYoksa() As Boolean
			Return Not Raporlar.Subset.Exists(DateStr & " >= tarih1 and " & DateStr & " <= tarih2")
		End Function

		Private Function YatirilanGunSayisi(ByVal Bordro As String) As Integer
			Dim Sonuc = Yatirimlar.Subset.Sum("pogs", "bordrotar = " & DateStr & " and bordro='" & Bordro & "'")
			If IsDBNull(Sonuc) Then Return 0
			Return Sonuc
		End Function

		Private Function ProblemliGunSayisi(ByVal Bordro As String) As Integer
			Dim Sonuc = ProblemliYatirimlar.Subset.Sum("pogs", "bordrotar = " & DateStr & " and bordro = '" & Bordro & "'")
			If IsDBNull(Sonuc) Then Return 0
			Return Sonuc
		End Function

		Private Function DigerIsyerindenYatirilanGunSayisi() As Integer
			Dim Sonuc = DigerYatirimlar.Subset.Sum("pogs", "bordrotar = " & DateStr & " and bordro in ('D','E')")
			If IsDBNull(Sonuc) Then Return 0
			Return Sonuc
		End Function

		Private Sub TablolariOku()
			Q("select a.sno,a.isyerino,girciktar as tarih1,a.statu,c.tehlike,")
			Q("isnull((select min(girciktar) from sigc b where a.sno = b.sno and a.isyerino = b.isyerino")
			Q("and a.girciktar < b.girciktar and b.gircik = 'C'),isnull(case when a.statu='01.01.002' then (select min(girciktar) from sigc d where a.sno = d.sno and a.girciktar < d.girciktar and d.gircik = 'G' and statu = '01.01.002') else null end,'2099/01/01')) as tarih2")
			Q("from sigc a inner join isyerigenel c on a.isyerino = c.isyerino")
			Q("where a.isyerino = @isyerino and gircik = 'G' order by girciktar")
			GirisCikis = Token.OpenSQL("*", isyerino)
			Token.ExecSQL("select distinct sno into #list from sigc where isyerino = @~isyerino", isyerino)
			Token.ExecSQL("select distinct sno into #isvlist from sigc where isyerino = @~isyerino and statu in ('01.01.003','01.01.004')", isyerino)
			Q("select a.sno,a.isyerino,girciktar as tarih1,a.statu,c.tehlike,")
			Q("isnull((select min(girciktar) from sigc b where a.sno = b.sno and a.isyerino = b.isyerino")
			Q("and a.girciktar < b.girciktar and b.gircik = 'C'),isnull(case when a.statu='01.01.002' then (select min(girciktar) from sigc d where a.sno = d.sno and a.girciktar < d.girciktar and d.gircik = 'G') else null end,'2099/01/01')) as tarih2")
			Q("from sigc a inner join isyerigenel c on a.isyerino = c.isyerino")
			Q("where a.isyerino <> @~isyerino and gircik = 'G' and sno in (select sno from #isvlist) order by girciktar")
			DigerGirisCikis = Token.OpenSQL("*", isyerino)
			Q("select sno,kimlik,adi as onadi,soyadi,rtrim(adi)+' '+rtrim(soyadi) as adi,dtar,")
			Q("isnull(maasbasdrm,'') as maasbasdrm,isnull(maasbtar,'19760101') as maasbtar")
			Q("from sgenel where sno in (select sno from #list)")
			Kisiler = Token.OpenSQL("*")
			Q("select sno,bordrotar,bordro,pek,pogs from shesk where isyerino = @isyerino order by 1")
			Yatirimlar = Token.OpenSQL("*", isyerino)
			Q("select * from (")
			Q("select 'has' as tur, sno, histbastar as tarih1, histsontar as tarih2 from shasrapor where sno in (select sno from #list) union all")
			Q("select 'isk' as tur, sno, histbastar as tarih1, histsontar as tarih2 from siskrapor where sno in (select sno from #list) union all")
			Q("select 'ana' as tur, sno, odebastar as tarih1, odesontar as tarih2 from sagecode where sno in (select sno from #list)")
			Q(") t where tarih1 is not null and tarih2 is not null")
			Raporlar = Token.OpenSQL("*")
			Q("select sno,bordrotar,bordro,pek,pogs from shesk where isyerino <> @~isyerino and sno in (select sno from #isvlist) order by 1")
			DigerYatirimlar = Token.OpenSQL("*", isyerino)
			Q("select sno,bordrotar,bordro,pek,pogs from sheskp where isyerino = @isyerino")
			ProblemliYatirimlar = Token.OpenSQL("*", isyerino)
			Q("select sno,ilkstar as tarih, isnull(basamak,1) as basamak from sgenel where sno in (select sno from #isvlist) union all")
			Q("select sno,bastar, basamak from sgelirbas where sno in (select sno from #isvlist) order by sno,tarih")
			Basamaklar = Token.OpenSQL("*")
			Token.ExecSQL("drop table #list")
			Token.ExecSQL("drop table #isvlist")
		End Sub

		Public Sub TutarlariHesapla()
			For Each Ay In Aylar.Values
				For Each Bordro In Ay.Bordrolar
					If Bordro.pek = 0 Then
						For Each Yatirim In Bordro.yatirimlar
							Select Case Bordro.tip
								Case "H", "J" : Yatirim.pek = Prim.Tutar(Token, Bordro.tarih, Len(Bordro.tip), Yatirim.basamak) * Yatirim.pogs / 30
								Case Else : Yatirim.pek = Prim.AsgariUcret(Token, Bordro.tarih, OdemeTarihi) * Yatirim.pogs / 30
							End Select
							Bordro.pek += Yatirim.pek
						Next
					End If
					Bordro.hesapla(Isyeri("tehlike"), OdemeTarihi)
				Next
			Next
		End Sub

		Public Sub BordroEkle(bordrotar As Date, tip As String, pek As Decimal)
			Dim bordro As New Bordro
			bordro.tarih = bordrotar
			bordro.tip = tip
			bordro.pek = pek
			bordro.hesapla(Isyeri("tehlike"), OdemeTarihi)
			bordro.virtual = True
			Bordrolar.Add(bordro)
		End Sub

		Sub DonemlereAyir()
			Donemler.Clear()
			For Each Bordro In Bordrolar
				If Not Aylar.ContainsKey(Bordro.tarih) Then Aylar.Add(Bordro.tarih, New Ay)
				Aylar(Bordro.tarih).Tarih = Bordro.tarih
				Aylar(Bordro.tarih).Bordrolar.Add(Bordro)
			Next
			For Each Ay In Aylar.Values
				Dim Donem As String = Trim(Ay.Tarih.Year) & "/" & Format(Ay.Tarih.Month, "00")
				If Not Donemler.ContainsKey(Donem) Then Donemler.Add(Donem, New Donem)
				Donemler(Donem).Donem = Donem
				Donemler(Donem).Aylar.Add(Ay)
			Next
			TutarlariHesapla()
		End Sub

	End Class

	Module Functions

		Function HesaplaIsveren(Token As Token, sno As Integer, sube As Integer, tarih1 As Date, tarih2 As Date, odemetar As Date) As Borc
			Dim Borc As New BorcIsveren With {.Token = Token}
			Borc.Hesapla(sno, sube, tarih1, tarih2, odemetar)
			Return Borc
		End Function

		Function HesaplaIsyeri(Token As Token, isyerino As String, tarih1 As Date, tarih2 As Date, odemetar As Date) As Borc
			Dim Borc As New BorcIsyeri With {.Token = Token}
			Borc.Hesapla(isyerino, tarih1, tarih2, odemetar)
			Return Borc
		End Function

	End Module

End Namespace
